//
//  LYLiveUnPlayingCell.m
//  LYLiveApp
//
//  Created by QD on 2018/6/25.
//  Copyright © 2018年 览益信息科技. All rights reserved.
//

#import "LYLiveUnPlayingCell.h"
#import "LYIntroduceModel.h"
#import "LYIntroduceService.h"
#import <AdSupport/AdSupport.h>
#import "LYOpenPushNoticeView.h"

@interface LYLiveUnPlayingCell ()<RequestDelegate>
@property (weak, nonatomic) IBOutlet UILabel *timeLabel;
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *nameLabelwidth;
@property (weak, nonatomic) IBOutlet UIButton *expandBtn;
@property (weak, nonatomic) IBOutlet UIButton *playbackBtn;
@property (weak, nonatomic) IBOutlet UILabel *descLabel;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *descLabelHeight;
@property (weak, nonatomic) IBOutlet UIButton *playbackingBtn;
@property (weak, nonatomic) IBOutlet UIButton *noticeBtn;
@property(nonatomic,strong) LYIntroduceService *introduceService;
@property (weak, nonatomic) IBOutlet UIView *upLine;
@property (weak, nonatomic) IBOutlet UIView *downLine;

@end

@implementation LYLiveUnPlayingCell

+ (instancetype)cellWithTableView:(UITableView *)tableView
{
    static NSString *identifier = @"LYLiveUnPlayingCell";
    LYLiveUnPlayingCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil)
    {
        cell = [[[NSBundle mainBundle]loadNibNamed:@"LYLiveUnPlayingCell" owner:self options:nil] lastObject];
    }
    return cell;
}


- (void)awakeFromNib {
    [super awakeFromNib];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction)];
    self.nameLabel.userInteractionEnabled = YES;
    [self.nameLabel addGestureRecognizer:tap];
    self.noticeBtn.backgroundColor = HEX(@"#F16B6F");
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

}

- (void)setRoomListModel:(LYRoomListModel *)roomListModel {
    
    _roomListModel = roomListModel;
    self.timeLabel.text = roomListModel.startTime;
    if (![LYUtil isNilOrEmpty:roomListModel.actTeacher]) {
        self.nameLabel.text = [NSString stringWithFormat:@"%@-%@",roomListModel.actName,roomListModel.actTeacher];
    } else {
        self.nameLabel.text = [NSString stringWithFormat:@"%@",roomListModel.actName];
    }
    self.nameLabelwidth.constant = [LYUtil sizeWithText:self.nameLabel.text font:self.nameLabel.font withWidth:200].width + 5;
    
    if (!roomListModel.isExpand) {
        self.descLabel.hidden = YES;
        self.upLine.hidden = NO;
        self.downLine.hidden = YES;
        self.expandBtn.selected = NO;
    } else {
        self.descLabel.hidden = NO;
        self.upLine.hidden = YES;
        self.downLine.hidden = NO;
        self.expandBtn.selected = YES;
        self.descLabel.text = roomListModel.foreshow;
    }
    
    CGFloat labelHeight = [LYUtil sizeWithText:roomListModel.foreshow font:self.descLabel.font withWidth:SCREEN_WIDTH - 65].height;
    if (labelHeight < 20) {
        labelHeight = 16;
    }
    self.descLabelHeight.constant = labelHeight;
    
    if (roomListModel.isPlaybacking) {
        self.playbackBtn.hidden = YES;
        self.noticeBtn.hidden = YES;
        self.playbackingBtn.hidden = NO;
        self.nameLabel.textColor = HEX(@"#d94b41");
    } else {
        self.nameLabel.textColor = HEX(@"#323232");
        if (roomListModel.actStatus.integerValue == 0) {   //未播
            self.noticeBtn.hidden = NO;
            self.playbackBtn.hidden = YES;
            self.playbackingBtn.hidden = YES;
            
            if (roomListModel.isSub.integerValue == 1) {
                [self.noticeBtn setTitle:@"取消提醒" forState:UIControlStateNormal];
                self.noticeBtn.backgroundColor = HEX(@"#bbbbbb");
                self.noticeBtn.selected = YES;
            } else {
                [self.noticeBtn setTitle:@"开播提醒" forState:UIControlStateNormal];
                self.noticeBtn.backgroundColor = HEX(@"#F16B6F");
                self.noticeBtn.selected = NO;
            }
            
        } else if (roomListModel.actStatus.integerValue == 1) {   //直播中
            
            
        } else if (roomListModel.actStatus.integerValue == 3) {   //已播放
            
            self.noticeBtn.hidden = YES;
            self.playbackBtn.hidden = NO;
            self.playbackingBtn.hidden = YES;
            
            if ([LYUtil isNilOrEmpty:roomListModel.vhallId]) {
                self.playbackBtn.hidden = YES;
            }
            
        }
    }
}

#pragma mark ------ Event Response
- (void)tapAction {
    
    [self expandAction:nil];
    
}

- (IBAction)playbackAction:(id)sender {
    
    !self.playBackBlock?:self.playBackBlock(self.roomListModel);
}

- (IBAction)expandAction:(UIButton *)button {
    
    if (!self.roomListModel.isExpand) {
        self.roomListModel.isExpand = YES;
    } else {
        self.roomListModel.isExpand = NO;
    }
    !self.expandBlock?:self.expandBlock();
}

- (IBAction)noticeAction:(UIButton *)button {
   
    
    UIUserNotificationSettings *setting = [[UIApplication sharedApplication] currentUserNotificationSettings];
    if (UIUserNotificationTypeNone == setting.types) {
        [LYOpenPushNoticeView showPushNoticeView];
        return;
    }
        
    WS(weakSelf)
    NSDictionary *parameters = @{@"actId":@(self.roomListModel.actId.integerValue),
                                 @"registrationId":[LYLocalData sharedInstance].registrationID,
                                 };

    if (!button.selected) {

        [self.introduceService addNotice:parameters block:^(BOOL success, id object) {
            if (success) {
                [button setTitle:@"取消提醒" forState:UIControlStateNormal];
                weakSelf.noticeBtn.backgroundColor = HEX(@"#bbbbbb");
                
                [LYUtil showSuccessHUD:@"已成功设置开播提醒" AddedTo:MAIN_WINDOW interval:1.5];
                weakSelf.roomListModel.isSub = @(1);
                button.selected = YES;
            } else {
                weakSelf.roomListModel.isSub = @(0);
                button.selected = NO;
            }
        }];
    } else {
        [self.introduceService cancelNotice:parameters block:^(BOOL success, id object) {
            if (success) {
                [button setTitle:@"开播提醒" forState:UIControlStateNormal];
                weakSelf.noticeBtn.backgroundColor = HEX(@"#F16B6F");
                
                [LYUtil showSuccessHUD:@"已成功取消开播提醒" AddedTo:MAIN_WINDOW interval:1.5];
                weakSelf.roomListModel.isSub = @(0);
                button.selected = NO;
            } else {
                weakSelf.roomListModel.isSub = @(1);
                button.selected = YES;
            }
        }];
    }
}

#pragma mark ----- getters and setters
-(LYIntroduceService *)introduceService{
    
    if (nil == _introduceService) {
        _introduceService = [[LYIntroduceService alloc] initWithDelegate:self];
    }
    return _introduceService;
}
@end
